//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.jusfoun.communication.util;

import java.io.Serializable;
import java.util.List;

public class PageInfo<T extends Serializable> implements Serializable {
    private static final long serialVersionUID = 1L;
    private int pageNum;
    private int pageSize;
    private int size;
    private int startRow;
    private int endRow;
    private long total;
    private int pages;
    private List<T> list;
    private int firstPage;
    private int prePage;
    private int nextPage;
    private int lastPage;
    private boolean isFirstPage;
    private boolean isLastPage;
    private boolean hasPreviousPage;
    private boolean hasNextPage;
    private int navigatePages = 8;
    private int[] navigatepageNums;

    public PageInfo() {
        this.isFirstPage = false;
        this.isLastPage = false;
        this.hasPreviousPage = false;
        this.hasNextPage = false;
    }

    /**
     * @param pageNum  页码
     * @param pageSize 每页显示数量
     * @param total    总共数据量
     */
    public PageInfo(Integer pageNum, Integer pageSize, Long total) {
        this();
        this.total = total;
        if (null == pageNum || null == pageSize || pageSize <= 0 || total <= 0) {
            return;
        }
        if (pageNum <= 0) {
            pageNum = 1;
        }
        this.pageNum = pageNum;
        this.pageSize = pageSize;
        calSize();
    }

    /**
     * @param list     数据列表(已经分页)
     * @param pageNum  页码
     * @param pageSize 每页显示数量
     * @param total    总共数据量
     */
    public PageInfo(List<T> list, Integer pageNum, Integer pageSize, Long total) {
        this();
        this.list = list;
        if (null != pageNum && null != pageSize && null != total && pageSize > 0) {
            this.pageNum = pageNum;
            this.pageSize = pageSize;
            this.total = total;
            calSize();
        }

    }

    /**
     * 计算分页相关信息
     */
    public void calSize() {
        if (null != list) {
            this.size = list.size();
        }
        if (pageSize <= 0 || total <= 0) {
            return;
        }
        startRow = pageNum * pageSize - pageSize + 1;
        endRow = null == list ? (startRow + pageSize - 1 > (int) total ? (int) total : startRow + pageSize - 1) : startRow + size - 1;
        pages = (int) (total / pageSize);
        if (total % pageSize != 0) {
            pages++;
        }
        calPages();
        calcNavigatepageNums();
    }

    private void calPages() {
        if (pages > 0) {
            firstPage = 1;
            lastPage = pages;
        }
        if (pageNum > 1) {
            prePage = pageNum - 1;
        }
        if (pages > pageNum) {
            nextPage = pageNum + 1;
        }
        isFirstPage = 1 == pageNum;
        isLastPage = pages == pageNum;
        hasPreviousPage = pageNum > 1;
        hasNextPage = pages > pageNum && pages > 1;
    }

    private void calcNavigatepageNums() {
        //当总页数小于或等于导航页码数时
        if (this.pages <= this.navigatePages) {
            this.navigatepageNums = new int[this.pages];
            for (int i = 0; i < this.pages; i++) {
                this.navigatepageNums[i] = i + 1;
            }
        } else { //当总页数大于导航页码数时
            this.navigatepageNums = new int[this.navigatePages];
            int startNum = this.pageNum - (this.navigatePages / 2);
            int endNum = this.pageNum + (this.navigatePages / 2);

            if (startNum < 1) {
                startNum = 1;
                //(最前navigatePages页
                for (int i = 0; i < this.navigatePages; i++) {
                    this.navigatepageNums[i] = startNum++;
                }
            } else if (endNum > this.pages) {
                endNum = this.pages;
                //最后navigatePages页
                for (int i = this.navigatePages - 1; i >= 0; i--) {
                    this.navigatepageNums[i] = endNum--;
                }
            } else {
                //所有中间页
                for (int i = 0; i < this.navigatePages; i++) {
                    this.navigatepageNums[i] = startNum++;
                }
            }
        }
    }


    public int getPageNum() {
        return this.pageNum;
    }

    public void setPageNum(int pageNum) {
        this.pageNum = pageNum;
    }

    public int getPageSize() {
        return this.pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public int getSize() {
        return this.size;
    }

    public void setSize(int size) {
        this.size = size;
    }

    public int getStartRow() {
        return this.startRow;
    }

    public void setStartRow(int startRow) {
        this.startRow = startRow;
    }

    public int getEndRow() {
        return this.endRow;
    }

    public void setEndRow(int endRow) {
        this.endRow = endRow;
    }

    public long getTotal() {
        return this.total;
    }

    public void setTotal(long total) {
        this.total = total;
    }

    public int getPages() {
        return this.pages;
    }

    public void setPages(int pages) {
        this.pages = pages;
    }

    public List<T> getList() {
        return this.list;
    }

    public void setList(List<T> list) {
        this.list = list;
    }

    public int getFirstPage() {
        return this.firstPage;
    }

    public void setFirstPage(int firstPage) {
        this.firstPage = firstPage;
    }

    public int getPrePage() {
        return this.prePage;
    }

    public void setPrePage(int prePage) {
        this.prePage = prePage;
    }

    public int getNextPage() {
        return this.nextPage;
    }

    public void setNextPage(int nextPage) {
        this.nextPage = nextPage;
    }

    public int getLastPage() {
        return this.lastPage;
    }

    public void setLastPage(int lastPage) {
        this.lastPage = lastPage;
    }

    public boolean isIsFirstPage() {
        return this.isFirstPage;
    }

    public void setIsFirstPage(boolean isFirstPage) {
        this.isFirstPage = isFirstPage;
    }

    public boolean isIsLastPage() {
        return this.isLastPage;
    }

    public void setIsLastPage(boolean isLastPage) {
        this.isLastPage = isLastPage;
    }

    public boolean isHasPreviousPage() {
        return this.hasPreviousPage;
    }

    public void setHasPreviousPage(boolean hasPreviousPage) {
        this.hasPreviousPage = hasPreviousPage;
    }

    public boolean isHasNextPage() {
        return this.hasNextPage;
    }

    public void setHasNextPage(boolean hasNextPage) {
        this.hasNextPage = hasNextPage;
    }

    public boolean isFirstPage() {
        return isFirstPage;
    }

    public void setFirstPage(boolean firstPage) {
        isFirstPage = firstPage;
    }

    public boolean isLastPage() {
        return isLastPage;
    }

    public void setLastPage(boolean lastPage) {
        isLastPage = lastPage;
    }

    public int getNavigatePages() {
        return navigatePages;
    }

    public void setNavigatePages(int navigatePages) {
        this.navigatePages = navigatePages;
    }

    public int[] getNavigatepageNums() {
        return navigatepageNums;
    }

    public void setNavigatepageNums(int[] navigatepageNums) {
        this.navigatepageNums = navigatepageNums;
    }
}
